package com.ophiux.cs.api.consultBase.impl;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.consultBase.port.IDeptSortsApiPort;
import com.ophiux.cs.application.consultBase.dto.*;
import com.ophiux.cs.application.consultBase.service.IAddDeptSortsAppService;
import com.ophiux.cs.application.consultBase.service.IDeleteDeptSortsAppService;
import com.ophiux.cs.application.consultBase.service.IGetDeptSortsAppService;
import com.ophiux.cs.application.consultBase.service.IUpdateDeptSortsAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.List;

@Service
@Slf4j
public class DeptSortsApiService implements IDeptSortsApiPort {
	@Autowired
    IAddDeptSortsAppService addDeptSortsAppService;
    @Autowired
    IUpdateDeptSortsAppService updateDeptSortsAppService;
    @Autowired
    IDeleteDeptSortsAppService deleteDeptSortsAppService;
    @Autowired
    IGetDeptSortsAppService getDeptSortsAppService;

    @Override
	public String createSorts(String data) {
		log.info("createDeptSorts接口,请求参数："+data);
        Request<AddDeptSortsRequest> request = null;
		Result<AddDeptSortsResponse> result = null;
        try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				@Override
				public Type getObtainType() {
					return new TypeToken<Request<AddDeptSortsRequest>>() {
					}.getType();
				}
			});
		} catch (Exception e) {
			log.info("createDeptSorts接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}

        AddDeptSortsRequest dto =request.getPARAM();
		result = addDeptSortsAppService.createDeptSorts(dto);

		String returnMessage = JsonUtils.toJson(result);
		log.info("createHospital接口,返回参数："+returnMessage);
		return returnMessage;
	}

    @Override
    public String updateDeptSorts(String data) {
        log.info("updateDeptSorts接口,请求参数："+data);
        Request<UpdateDeptSortsRequest> request = null;
        Result<UpdateDeptSortsResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<UpdateDeptSortsRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("updateDeptSorts接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        UpdateDeptSortsRequest dto =request.getPARAM();
        result = updateDeptSortsAppService.updateDeptSorts(dto);
        String returnMessage = JsonUtils.toJson(result);
        log.info("updateDeptSorts接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String deleteDeptSorts(String data) {
        log.info("deleteDeptSorts接口,请求参数："+data);
        Request<DeleteDeptSortsRequest> request = null;
        Result<DeleteDeptSortsResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<DeleteDeptSortsRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("deleteDeptSorts接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        DeleteDeptSortsRequest dto =request.getPARAM();
        result = deleteDeptSortsAppService.deleteDeptSorts(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("deleteDeptSorts接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String GetDeptSorts(String data) {
        log.info("【GetDeptSorts】接口,请求参数："+data);
        Request<GetDeptSortsRequest> request = null;
        Result<ResponsePage<GetDeptSortsResponse>> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetDeptSortsRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("getHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetDeptSortsRequest dto =request.getPARAM();
        result = getDeptSortsAppService.getDeptSorts(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("【GetDeptSorts】接口,返回参数："+returnMessage);
        return returnMessage;
    }

}
